package ldh.im.fxbase.component;

import javafx.beans.InvalidationListener;
import javafx.beans.property.DoubleProperty;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.scene.layout.AnchorPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Screen;

public class MaskPane extends AnchorPane {

    private DoubleProperty xProperty = new SimpleDoubleProperty(0);
    private DoubleProperty yProperty = new SimpleDoubleProperty(0);

    private DoubleProperty widthProperty = new SimpleDoubleProperty(0);
    private DoubleProperty heightProperty = new SimpleDoubleProperty(0);

    private Rectangle topRectangle = new Rectangle();
    private Rectangle bottomRectangle = new Rectangle();
    private Rectangle leftRectangle = new Rectangle();
    private Rectangle rightRectangle = new Rectangle();

    private ObjectProperty<Color> fillColor = new SimpleObjectProperty<>(Color.GRAY);

    public MaskPane() {
        fillChange();

        this.getChildren().addAll(topRectangle, rightRectangle, bottomRectangle, leftRectangle);
        bindEvent();
    }

    public DoubleProperty xProperty() {
        return xProperty;
    }

    public DoubleProperty yProperty() {
        return yProperty;
    }

    public DoubleProperty hProperty() {
        return heightProperty;
    }

    public DoubleProperty wProperty() {
        return widthProperty;
    }

    private void bindEvent() {
        xProperty.addListener((InvalidationListener)l->reLayouts());
        yProperty.addListener((InvalidationListener)l->reLayouts());
        widthProperty.addListener((InvalidationListener)l->reLayouts());
        heightProperty.addListener((InvalidationListener)l->reLayouts());
        fillColor.addListener((InvalidationListener)l->fillChange());
    }

    private void fillChange() {
        topRectangle.setFill(fillColor.getValue());
        rightRectangle.setFill(fillColor.getValue());
        bottomRectangle.setFill(fillColor.getValue());
        leftRectangle.setFill(fillColor.getValue());
    }

    private void reLayouts() {
        Screen screen = getScreen();
        if (screen == null) return;
        this.setPrefWidth(screen.getBounds().getWidth());
        this.setPrefHeight(screen.getBounds().getHeight());
        this.setMinWidth(screen.getBounds().getWidth());
        this.setMinHeight(screen.getBounds().getHeight());

        topRectangle.setWidth(screen.getBounds().getWidth());
        topRectangle.setHeight(yProperty.getValue());
        AnchorPane.setTopAnchor(topRectangle, 0.0);
        AnchorPane.setLeftAnchor(topRectangle, 0.0);

        rightRectangle.setWidth(screen.getBounds().getWidth()-xProperty.get()-widthProperty.get());
        rightRectangle.setHeight(heightProperty.getValue());
        AnchorPane.setTopAnchor(rightRectangle, yProperty.getValue());
        AnchorPane.setLeftAnchor(rightRectangle, xProperty.get()+widthProperty.get());

        bottomRectangle.setWidth(screen.getBounds().getWidth());
        bottomRectangle.setHeight(screen.getBounds().getHeight()-yProperty.getValue()-heightProperty.get());
        AnchorPane.setTopAnchor(bottomRectangle, yProperty.getValue()+heightProperty.get());
        AnchorPane.setLeftAnchor(bottomRectangle, 0.0);

        leftRectangle.setWidth(xProperty.getValue());
        leftRectangle.setHeight(heightProperty.getValue());
        AnchorPane.setTopAnchor(leftRectangle, yProperty.getValue());
        AnchorPane.setLeftAnchor(leftRectangle, 0.0);
    }

    private Screen getScreen() {
        for(Screen screen : Screen.getScreens()) {
            if (screen.getBounds().getMaxX() > xProperty.get() && screen.getBounds().getMaxY() > yProperty.get()) {
                return screen;
            }
        }
        return null;
    }
}
